package com.banggood.ppsprojectarch.common;


import com.banggood.pps.commons.bean.oa.OAUserInfo;
import com.banggood.pps.commons.exception.BusinessException;
import com.banggood.pps.commons.exception.GlobalBusinessEnum;

/**
 * 用于存储当前用户数据
 * @author weihuajiang
 * @date 2017/10/17
 */
public class OaUserInfoThreadLocal {

    private static ThreadLocal<OAUserInfo> oaUserInfoThreadLocal = new ThreadLocal<OAUserInfo>();

    public static boolean checkThreadLocal() {
        boolean existThreadLocal = false;
        if (oaUserInfoThreadLocal.get() != null) {
            existThreadLocal = true;
        }
        return existThreadLocal;
    }

    public static OAUserInfo getUserInfo() throws Exception {

        if (oaUserInfoThreadLocal.get() == null) {
           throw new BusinessException(GlobalBusinessEnum.USER_NOT_LOGIN);
        } else {
            return oaUserInfoThreadLocal.get();
        }
    }

    public static void setOaUserInfo(OAUserInfo oaUserInfo) {
        oaUserInfoThreadLocal.set(oaUserInfo);
    }

    public static void removeOaUserInfo() {
        oaUserInfoThreadLocal.remove();
    }

}
